package com.kunka.controller.system;



import com.kunka.api.entity.common.Result;
import com.kunka.api.entity.system.TSysMenu;
import com.kunka.api.service.service.system.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * 菜单控制器类
 */
@Api(tags = "菜单相关逻辑")
@RestController
@RequestMapping("system")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 跳转菜单列表页面
     * @param model
     * @return
     */
    @ApiOperation("跳转菜单列表")
    @GetMapping("/menu")
    public ModelAndView toMenuPage(Model model){

        ModelAndView modelAndView = new  ModelAndView();

        modelAndView.setViewName("system/menu/menu-list");

        List<TSysMenu> menuTreeList = menuService.queryMenuList();

        menuTreeList = menuService.convertTreeList(menuTreeList);

        model.addAttribute("menuList",menuTreeList);

        return modelAndView;
    }


    /**
     * 跳转 添加 或者修改页面
     * @param menuId
     * @param type
     * @param model
     * @return
     */
    @ApiOperation("跳转菜单修改/添加页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true),
            @ApiImplicitParam(name = "type", value = "类型 0 添加 1 修改", required = true)
    })
    @GetMapping("/menu/{menuId}/{type}")
    public ModelAndView toEditMenuPage(@PathVariable(value = "menuId") Integer menuId,@PathVariable(value = "type") Integer type, Model model){

        ModelAndView modelAndView = new  ModelAndView();

        String typeSuffix = (type == 0) ? "add":"edit";

        modelAndView.setViewName("system/menu/menu-"+typeSuffix);

        TSysMenu menu = menuService.getMenu(menuId);

        model.addAttribute("menu",menu);

        return modelAndView;

    }


    /**
     * 修改菜单操作
     * @param menu
     * @return
     */
    @ApiOperation("菜单修改后台逻辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu", value = "菜单对象", required = true)
    })
    @PutMapping("/menu")
    public Result editMenu(TSysMenu menu){

        menuService.updateMenu(menu);

        return new Result(0);

    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @ApiOperation("添加菜单后台逻辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menu", value = "菜单对象", required = true)
    })
    @PostMapping("/menu")
    public Result addMenu(TSysMenu menu){

        menuService.addMenu(menu);

        return new Result(0);
    }


    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @ApiOperation("删除菜单后台逻辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true)
    })
    @DeleteMapping("/menu/{menuId}")
    public Result delMenu(@PathVariable(value = "menuId") Integer menuId){


        //todo:校验时候有角色已经关联了菜单
        menuService.delMenu(menuId);

        return new Result(0);

    }









}
